/*
 * This is Melet, a salary slip designer. Feed it with data regarding to salary payment, prepare it for print through the graphical presentation, and you have it done.
 * Copyright (C) 2014, AFDiaX
 * 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 * This program is based on UMLet, a Free UML Tool for Fast UML Diagrams.
 * To contact the authors of UMLet visit https://code.google.com/p/umlet/people/list
 * To contact the authors of Melet visit https://gna.org/users/gabocze
 */
/**
 * 
 */
package com.baselet.gui.helper;

import java.util.Collection;
import java.util.Iterator;
import java.util.Map.Entry;

import javax.swing.JComponent;

import com.baselet.element.interfaces.GridElement;

/**
 * Provides a view of the underlying Iterator as thought its elements were of type JComponent.
 * We get this view out of the underlying Iterator, a GridElement's one.
 * 
 * Usage: 1. Use your undecorated Collection normaly
 * 2. Initialize this instance with that, and
 * 3. Pass this instance to clients dealing with the decorated view
 * This is a view only iterator, then be all the modifying operations disabled, for itself and for it's elements either (UnsupportedOperationException).
 * @author Clara Yael Bradichansky
 */
public class DecoratedJComponentTwins implements
		Iterable<Entry<JComponent, JComponent>> {
	private Collection<Entry<JComponent,GridElement>>underlying;
	/**
	 * @param underlying
	 */
	public DecoratedJComponentTwins(
			Collection<Entry<JComponent, GridElement>> underlying) {
		super();
		if(underlying==null)
			throw new NullPointerException("Must provide an underlying parameter.");
		this.underlying = underlying;
	}



	@Override
	public Iterator<Entry<JComponent, JComponent>> iterator() {
		final Iterator<Entry<JComponent,GridElement>> underlyingIt = underlying.iterator();
		return new Iterator<Entry<JComponent,JComponent>>() {
			/* (non-Javadoc)
			 * @see java.util.Iterator#hasNext()
			 */
			@Override
			public boolean hasNext() {
				return underlyingIt.hasNext();
			}

			/* (non-Javadoc)
			 * @see java.util.Iterator#next()
			 */
			@Override
			public Entry<JComponent, JComponent> next() {
				final Entry<JComponent,GridElement> next = underlyingIt.next();
				Entry<JComponent,JComponent> decoratedNext = new Entry<JComponent, JComponent>() {
					
					@Override
					public JComponent setValue(JComponent value) {
						throw new UnsupportedOperationException("Only view entry.");
					}
					
					@Override
					public JComponent getValue() {
						return (JComponent)next.getValue().getComponent();
					}
					
					@Override
					public JComponent getKey() {
						return next.getKey();
					}
				};
				return decoratedNext;
			}

			/* (non-Javadoc)
			 * @see java.util.Iterator#remove()
			 */
			@Override
			public void remove() {
				throw new UnsupportedOperationException("Only view Iterator.");
			}
		};
	}
	
	

}
